Completed
Pull Request — master (#13)
by Justin
07:02
created

Subject.getIdentifiers   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 3
Ratio 100 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
c 1
b 0
f 0
nc 1
nop 0
dl 3
loc 3
rs 10
1 View Code Duplication
var Conclusion = require('./Conclusion'),
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
2
    Identifiers = require('./Identifiers'),
3
    EvidenceReference = require('./EvidenceReference'),
4
    SourceReference = require('./SourceReference'),
5
    utils = require('./utils');
6
7
/**
8
 * An object identified in time and space by various conclusions.
9
 * 
10
 * @constructor
11
 * @param {Object} [json]
0 ignored issues
show
Documentation introduced by
The parameter [json] does not exist. Did you maybe forget to remove this comment?
Loading history...
12
 */
13
var Subject = function(json){
14
  
15
  // Protect against forgetting the new keyword when calling the constructor
16
  if(!(this instanceof Subject)){
17
    return new Subject(json);
18
  }
19
  
20
  // If the given object is already an instance then just return it. DON'T copy it.
21
  if(Subject.isInstance(json)){
22
    return json;
23
  }
24
  
25
  Conclusion.call(this, json);
26
  
27
  if(json){
0 ignored issues
show
Complexity Best Practice introduced by
There is no return statement if json is false. Are you sure this is correct? If so, consider adding return; explicitly.

This check looks for functions where a return statement is found in some execution paths, but not in all.

Consider this little piece of code

function isBig(a) {
    if (a > 5000) {
        return "yes";
    }
}

console.log(isBig(5001)); //returns yes
console.log(isBig(42)); //returns undefined

The function isBig will only return a specific value when its parameter is bigger than 5000. In any other case, it will implicitly return undefined.

This behaviour may not be what you had intended. In any case, you can add a return undefined to the other execution path to make the return value explicit.

Loading history...
28
    this.setExtracted(json.extracted);
29
    this.setEvidence(json.evidence);
30
    this.setIdentifiers(json.identifiers);
31
    this.setMedia(json.media);
0 ignored issues
show
Best Practice introduced by
There is no return statement in this branch, but you do return something in other branches. Did you maybe miss it? If you do not want to return anything, consider adding return undefined; explicitly.
Loading history...
32
  }
33
};
34
35
Subject.prototype = Object.create(Conclusion.prototype);
36
37
Subject._gedxClass = Subject.prototype._gedxClass = 'GedcomX.Subject';
38
39
/**
40
 * Check whether the given object is an instance of this class.
41
 * 
42
 * @param {Object} obj
43
 * @returns {Boolean}
44
 */
45
Subject.isInstance = function(obj){
46
  return utils.isInstance(obj, this._gedxClass);
47
};
48
49
/**
50
 * Is this an extracted conclusion?
51
 * 
52
 * @returns {Boolean} extracted
53
 */
54
Subject.prototype.isExtracted = function(){
55
  // Double exclamations force a boolean no matter what type the property
56
  // currently is. The main reason for this is to force undefiend into false.
57
  return !!this.isExtracted;
58
};
59
60
/**
61
 * Set the extracted property
62
 * 
63
 * @param {Boolean} extracted
64
 * @returns {Subject} This instance.
65
 */
66
Subject.prototype.setExtracted = function(extracted){
67
  // Double exclamations force a boolean
68
  this.extracted = !!extracted;
69
  return this;
70
};
71
72
/**
73
 * Get the evidence.
74
 * 
75
 * @returns {EvidenceReference[]}
76
 */
77
Subject.prototype.getEvidence = function(){
78
  return this.evidence || [];
79
};
80
81
/**
82
 * Set the evidence. This method will replace existing evidence entries with new entries.
83
 * 
84
 * @param {Object[]|EvidenceReference[]}
0 ignored issues
show
Documentation introduced by
The parameter * does not exist. Did you maybe forget to remove this comment?
Loading history...
85
 * @returns {Subject} This instance.
86
 */
87
Subject.prototype.setEvidence = function(evidence){
88
  if(Array.isArray(evidence)){
89
    this.evidence = [];
90
    var subject = this;
91
    evidence.forEach(function(e){
92
      subject.addEvidence(e);
93
    });
94
  }
95
  return this;
96
};
97
98
/**
99
 * Add evidence
100
 * 
101
 * @param {Object|EvidenceReference}
0 ignored issues
show
Documentation introduced by
The parameter * does not exist. Did you maybe forget to remove this comment?
Loading history...
102
 * @returns {Subject} This instance.
103
 */
104
Subject.prototype.addEvidence = function(evidence){
105
  if(evidence){
106
    if(!Array.isArray(this.evidence)){
107
      this.evidence = [];
108
    }
109
    this.evidence.push(EvidenceReference(evidence));
110
  }
111
  return this;
112
};
113
114
/**
115
 * Get the identifiers
116
 * 
117
 * @return {Identifiers}
118
 */
119
Subject.prototype.getIdentifiers = function(){
120
  return this.identifiers;
121
};
122
123
/**
124
 * Set the identifiers
125
 * 
126
 * @param {Object|Identifiers}
0 ignored issues
show
Documentation introduced by
The parameter * does not exist. Did you maybe forget to remove this comment?
Loading history...
127
 * @returns {Subject} This instance
128
 */
129
Subject.prototype.setIdentifiers = function(identifiers){
130
  if(identifiers){
131
    this.identifiers = Identifiers(identifiers);
132
  }
133
  return this;
134
};
135
136
/**
137
 * Get the media references
138
 * 
139
 * @returns {SourceReference[]}
140
 */
141
Subject.prototype.getMedia = function(){
142
  return this.media || [];
143
};
144
145
/**
146
 * Set the media references
147
 * 
148
 * @param {Object[]|SourceReference[]}
149
 */
150
Subject.prototype.setMedia = function(media){
151
  if(Array.isArray(media)){
152
    this.media = [];
153
    var subject = this;
154
    media.forEach(function(e){
155
      subject.addMedia(e);
156
    });
157
  }
158
  return this;
159
};
160
161
/**
162
 * Add media
163
 * 
164
 * @param {Object|SourceReference}
0 ignored issues
show
Documentation introduced by
The parameter * does not exist. Did you maybe forget to remove this comment?
Loading history...
165
 * @returns {Subject} This instance.
166
 */
167
Subject.prototype.addMedia = function(media){
168
  if(media){
169
    if(!Array.isArray(this.media)){
170
      this.media = [];
171
    }
172
    this.media.push(SourceReference(media));
173
  }
174
  return this;
175
};
176
177
/**
178
 * Export the object as JSON
179
 * 
180
 * @return {Object} JSON object
181
 */
182
Subject.prototype.toJSON = function(){
183
  var json = Conclusion.prototype.toJSON.call(this);
184
  
185
  if(this.extracted){
186
    json.extracted = this.extracted;
187
  }
188
  
189
  if(this.evidence){
190
    json.evidence = this.evidence.map(function(e){
191
      return e.toJSON();
192
    });
193
  }
194
  
195
  if(this.identifiers){
196
    json.identifiers = this.identifiers.toJSON();
197
  }
198
  
199
  if(this.media){
200
    json.media = this.media.map(function(m){
201
      return m.toJSON();
202
    });
203
  }
204
  
205
  return json;
206
};
207
208
module.exports = Subject;